package com.yiren.dbaa.module.practice_set;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.basestation.BaseStationHomeFragment;
import com.yiren.dbaa.module.practice_set.carmodel.view.CarModelHomeFragment;
import com.yiren.dbaa.module.practice_set.carmodel.view.activity.OpenCarModelActivity;
import com.yiren.dbaa.module.practice_set.device.DeviceSetFragment;
import com.yiren.dbaa.module.practice_set.line.view.activity.OpenLineActivity;
import com.yiren.dbaa.module.practice_set.line.view.fragment.LineHomeFragment;
import com.yiren.dbaa.module.practice_set.map.view.activity.OpenMapActivity;
import com.yiren.dbaa.module.practice_set.map.view.fragment.MapHomeFragment;
import com.yiren.dbaa.module.practice_set.site.view.activity.OpenSiteRuleActivity;
import com.yiren.dbaa.module.practice_set.site.view.adapter.CarPracticeSetNameAdapter;
import com.yiren.dbaa.module.practice_set.site.view.fragment.SiteRuleFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/19 14:46
 * @description: 练车设置首页
 */
public class PracticeCarSetActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.lv_set_name)
    ListView lvSetName;
    @BindView(R.id.fl_content)
    FrameLayout flContent;

    private SiteRuleFragment siteFragment;
    private LineHomeFragment lineHomeFragment;
    private MapHomeFragment mapHomeFragment;
    private CarModelHomeFragment carModelHomeFragment;
    private DeviceSetFragment deviceSetFragment;
    private BaseStationHomeFragment baseStationHomeFragment;

    private boolean isPartner;
    private int currentTab = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice_car_set);
        ButterKnife.bind(this);
        tvTitle.setText("练车设置");
        init();
        selectTab(0);
    }

    private void init() {
        isPartner = LoadConfigApi.loadUserParamConfig(this).isPartner();
        List<String> setNameList = new ArrayList<>();
        if (isPartner) {
            setNameList.add("考场规则");
            setNameList.add("线路数据");
            setNameList.add("高精地图");
            setNameList.add("车辆模型");
            setNameList.add("设备设置");
            setNameList.add("基站管理");
        } else {
            setNameList.add("考场规则");
            setNameList.add("线路数据");
            setNameList.add("高精地图");
            setNameList.add("车辆模型");
            setNameList.add("设备设置");
        }
        CarPracticeSetNameAdapter carPracticeSetNameAdapter = new CarPracticeSetNameAdapter(this, setNameList);
        lvSetName.setAdapter(carPracticeSetNameAdapter);
        lvSetName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                carPracticeSetNameAdapter.changeSelected(position);
                selectTab(position);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 设置首页按钮
     */
    private void setBtn(boolean isShow) {
        if (isShow) {
            tvRight.setVisibility(View.VISIBLE);
            switch (currentTab) {
                case 0:
                    tvRight.setText("已公开给教练的考规");
                    break;
                case 1:
                    tvRight.setText("已公开给教练的线路");
                    break;
                case 2:
                    tvRight.setText("已公开给教练的地图");
                    break;
                case 3:
                    tvRight.setText("已公开给教练的车模");
                    break;
            }
        } else {
            tvRight.setVisibility(View.GONE);
        }
    }

    /**
     * 进行选中Tab的处理
     */
    private void selectTab(int i) {
        currentTab = i;
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            //当选中点击的是第一页的Tab时
            case 0:
                setBtn(isPartner);
                //如果第一页对应的Fragment没有实例化，则进行实例化，并显示出来
                if (siteFragment == null) {
                    siteFragment = new SiteRuleFragment();
                    transaction.add(R.id.fl_content, siteFragment);
                } else {
                    //如果第一页对应的Fragment已经实例化，则直接显示出来
                    transaction.show(siteFragment);
                }
                break;
            case 1:
                setBtn(isPartner);
                if (lineHomeFragment == null) {
                    lineHomeFragment = new LineHomeFragment();
                    transaction.add(R.id.fl_content, lineHomeFragment);
                } else {
                    transaction.show(lineHomeFragment);
                }
                break;
            case 2:
                setBtn(isPartner);
                    if (mapHomeFragment == null) {
                        mapHomeFragment = new MapHomeFragment();
                        transaction.add(R.id.fl_content, mapHomeFragment);
                    } else {
                        transaction.show(mapHomeFragment);
                    }
                break;
            case 3:
                setBtn(isPartner);
                if (carModelHomeFragment == null) {
                    carModelHomeFragment = new CarModelHomeFragment();
                    transaction.add(R.id.fl_content, carModelHomeFragment);
                } else {
                    transaction.show(carModelHomeFragment);
                }
                break;
            case 4:
                setBtn(false);
                if (deviceSetFragment == null) {
                    deviceSetFragment = new DeviceSetFragment();
                    transaction.add(R.id.fl_content, deviceSetFragment);
                } else {
                    transaction.show(deviceSetFragment);
                }
                break;
            case 5:
                setBtn(false);
                if (baseStationHomeFragment == null) {
                    baseStationHomeFragment = new BaseStationHomeFragment();
                    transaction.add(R.id.fl_content, baseStationHomeFragment);
                } else {
                    transaction.show(baseStationHomeFragment);
                }
                break;
        }
        transaction.commit();
    }


    //将Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (siteFragment != null) {
            transaction.hide(siteFragment);
        }
        if (lineHomeFragment != null) {
            transaction.hide(lineHomeFragment);
        }
        if (mapHomeFragment != null) {
            transaction.hide(mapHomeFragment);
        }
        if (carModelHomeFragment != null) {
            transaction.hide(carModelHomeFragment);
        }
        if (baseStationHomeFragment != null) {
            transaction.hide(baseStationHomeFragment);
        }
        if (deviceSetFragment != null) {
            transaction.hide(deviceSetFragment);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
            case R.id.tv_right:
                switch (currentTab) {
                    case 0:
                        startActivity(new Intent(this, OpenSiteRuleActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(this, OpenLineActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(this, OpenMapActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(this, OpenCarModelActivity.class));
                        break;
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
